<template>
  <div class="app-container">
    <el-form
      ref="form"
      class="edit-form"
      :rules="rules"
      :model="form"
      label-width="100px"
    >
      <el-form-item label="店铺" prop="shopId">
        <el-select
          clearable
          filterable
          v-model="form.shopId"
          placeholder="请选择店铺"
        >
          <el-option
            v-for="item in shopList"
            :label="item.shopName"
            :value="item.id"
            :key="item.id"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="省份" prop="province">
        <el-input v-model="form.province" placeholder="请输入省份"></el-input>
      </el-form-item>

      <!-- 备注 -->
      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="form.remark"
          type="textarea"
          placeholder="请输入备注"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">{{
          form.id ? "保存修改" : "立即创建"
        }}</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {
  getAreaShopDetail,
  editAreaShop,
  addAreaShop,
  getShopList,
} from "@/api/shop/index";
import { regionData } from "element-china-area-data";

export default {
  data() {
    const options = regionData.map((item) => {
      return {
        label: item.label,
        value: item.value,
      };
    });

    return {
      options,
      selectedOptions: [],
      form: {
        shopId: "",
        province: "",
        city: "",
        remark: "",
      },
      rules: {
        province: [
          { required: true, message: "请选择省份", trigger: "change" },
        ],
        shopId: [{ required: true, message: "请选择店铺", trigger: "change" }],
      },
      shopList: [],
    };
  },
  created() {
    if (this.$route.query.id) {
      this.getDetail();
    }

    getShopList({}, 1, 999999).then((res) => {
      let shopListMap = {};
      res.data.records.forEach((item) => {
        shopListMap[item.id] = item;
      });
      this.shopList = res.data.records;
      this.shopListMap = shopListMap;
    });
  },
  methods: {
    handleChange() {
      this.form.provinces = this.selectedOptions[0];
      this.form.city = this.selectedOptions[1];
    },
    onSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.loading = true;
          const fn = this.form.id ? editAreaShop : addAreaShop;
          fn({
            ...this.form,
            status: this.form.status ? this.form.status : 2,
            type: 1,
          })
            .then((res) => {
              this.loading = false;
              this.$message.success("操作成功");
              this.$router.push("/shop/dyOrderAreaConfig");
            })
            .catch(() => {
              this.loading = false;
            });
        } else {
          return false;
        }
      });
    },
    getDetail() {
      getAreaShopDetail(this.$route.query.id).then((res) => {
        this.form = res.data;
        this.selectedOptions = [
          this.form.provinces,
          this.form.city,
          this.form.country,
        ];
      });
    },
  },
};
</script>
